//
//  CircleListMainViewCell.m
//  RACExample
//
//  Created by 山刘 on 2020/6/3.
//  Copyright © 2020 山刘. All rights reserved.
//

#import "CircleListMainViewCell.h"

@implementation CircleListMainViewCell
#pragma mark - init Views
+(NSString*)reuseIdentifier{
    return NSStringFromClass([self class]);
}

+(CircleListMainViewCell*)cellWithTableView:(UITableView*)tableView indexPath:(NSIndexPath*)indexPath{
    CircleListMainViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self reuseIdentifier]];
    if (!cell) {
        cell = [[CircleListMainViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[self reuseIdentifier]];
    }
    cell.indexPath = indexPath;
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self segInitViews];
        [self updateConstraints];
    }
    return self;
}

-(void)segInitViews{
    [self.contentView addSubview:self.raiseButton];
}

#pragma mark - Layout
- (void)updateConstraints {
    [self.raiseButton sizeToFit];
    [self.raiseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(self.contentView).mas_offset(-5);
    }];
    [super updateConstraints];
}

#pragma mark - RAC Data Binding
- (void)bindViewModel {
    self.textLabel.text = self.viewModel.model.title;
    //self.raiseButton.rac_command = self.viewModel.
    //RAC(self.textLabel,text) = RACObserve(self.viewModel.model, title); ==> 这个在数据刷新的时候报错！因为KVO的keypath重复绑定。
    //RAC(self.textLabel,text) = [RACObserve(self.viewModel.model, title) takeUntil:self.rac_prepareForReuseSignal]; ==> 可以通过这样的方式搞定。
    //在cell里面创建的信号加上takeUntil:cell.rac_prepareForReuseSignal，这个是让cell在每次重用的时候都去disposable创建的信号。
    
//    @weakify(self);
//    [[self.raiseButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
//        @strongify(self);
//        [self.viewModel.buttonClickSubject sendNext:self.viewModel.model];
//    }];
    //按照上面写的话，当cell重新进行 cellForRowAtIndexPath 时，会重复订阅，导致方法重复调用，可以这样写：
    @weakify(self);
    [[[self.raiseButton rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_prepareForReuseSignal] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self.viewModel.buttonClickSubject sendNext:self.viewModel.model.title];
    }];
}

#pragma mark - getter

#pragma mark setter
-(void)setViewModel:(CircleListMainViewCellViewModel *)viewModel{
    _viewModel = viewModel;
    if (!viewModel)  return;
    [self bindViewModel];
}

-(UIButton *)raiseButton{
    if (!_raiseButton) {
        _raiseButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_raiseButton setTitle:@"不同意" forState:UIControlStateNormal];
        _raiseButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        [_raiseButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _raiseButton.layer.borderColor = [UIColor blackColor].CGColor;
        _raiseButton.layer.borderWidth = .5;
        _raiseButton.layer.cornerRadius = 5;
        _raiseButton.layer.masksToBounds = YES;
        _raiseButton.backgroundColor = UIColor.yellowColor;
    }
    return _raiseButton;
}
@end
